package com.ruoyi.consultation.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.consultation.domain.LabConsultationSubject;
import com.ruoyi.consultation.mapper.LabConsultationSubjectMapper;
import com.ruoyi.consultation.service.ILabConsultationSubjectService;

/**
 * 咨询栏目Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-28
 */
@Service
public class LabConsultationSubjectServiceImpl implements ILabConsultationSubjectService {
	@Autowired
	private LabConsultationSubjectMapper labConsultationSubjectMapper;

	/**
	 * 查询咨询栏目
	 * 
	 * @param subjectId
	 *            咨询栏目ID
	 * @return 咨询栏目
	 */
	@Override
	public LabConsultationSubject selectLabConsultationSubjectById(Long subjectId) {
		return labConsultationSubjectMapper.selectLabConsultationSubjectById(subjectId);
	}

	/**
	 * 查询咨询栏目列表
	 * 
	 * @param labConsultationSubject
	 *            咨询栏目
	 * @return 咨询栏目
	 */
	@Override
	public List<LabConsultationSubject> selectLabConsultationSubjectList(
			LabConsultationSubject labConsultationSubject) {
		return labConsultationSubjectMapper.selectLabConsultationSubjectList(labConsultationSubject);
	}

	/**
	 * 新增咨询栏目
	 * 
	 * @param labConsultationSubject
	 *            咨询栏目
	 * @return 结果
	 */
	@Override
	public int insertLabConsultationSubject(LabConsultationSubject labConsultationSubject) {
		if(labConsultationSubject.getParentId()!=null){
			LabConsultationSubject info = labConsultationSubjectMapper
					.selectLabConsultationSubjectById(labConsultationSubject.getParentId());
			if(info==null){
				throw new CustomException("所选择的父栏目不存在");
			}
			// 如果父节点不为正常状态,则不允许新增子节点
			if (info.getStatusOpt()!=1) {
				throw new CustomException("父栏目停用，不能增加子栏目");
			}
			labConsultationSubject.setAncestors( info.getAncestors()+ "," + labConsultationSubject.getParentId());
		}else{
			labConsultationSubject.setParentId(Long.valueOf(0));
			labConsultationSubject.setAncestors("0");
		}
		return labConsultationSubjectMapper.insertLabConsultationSubject(labConsultationSubject);
	}

	/**
	 * 修改咨询栏目
	 * 
	 * @param labConsultationSubject
	 *            咨询栏目
	 * @return 结果
	 */
	@Override
	public int updateLabConsultationSubject(LabConsultationSubject labConsultationSubject) {
		LabConsultationSubject newParentDept = labConsultationSubjectMapper
				.selectLabConsultationSubjectById(labConsultationSubject.getParentId());
		LabConsultationSubject oldDept = labConsultationSubjectMapper
				.selectLabConsultationSubjectById(labConsultationSubject.getSubjectId());
		if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
			String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getSubjectId();
			String oldAncestors = oldDept.getAncestors();
			labConsultationSubject.setAncestors(newAncestors);
			updateDeptChildren(labConsultationSubject.getSubjectId(), newAncestors, oldAncestors);
		}
		return labConsultationSubjectMapper.updateLabConsultationSubject(labConsultationSubject);
	}

	/**
	 * 批量删除咨询栏目
	 * 
	 * @param subjectIds
	 *            需要删除的咨询栏目ID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabConsultationSubjectByIds(Long[] subjectIds) {
		return labConsultationSubjectMapper.deleteLabConsultationSubjectByIds(subjectIds);
	}*/

	/**
	 * 删除咨询栏目信息
	 * 
	 * @param subjectId
	 *            咨询栏目ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabConsultationSubjectById(Long subjectId) {
		this.labConsultationSubjectMapper.deleteChildrenById(subjectId);
		return labConsultationSubjectMapper.deleteLabConsultationSubjectById(subjectId);
	}

	/**
	 * 构建前端所需要树结构
	 * 
	 * @param depts
	 *            部门列表
	 * @return 树结构列表
	 */
	// @Override
	public List<LabConsultationSubject> buildDeptTree(List<LabConsultationSubject> depts) {
		List<LabConsultationSubject> returnList = new ArrayList<LabConsultationSubject>();
		List<Long> tempList = new ArrayList<Long>();
		for (LabConsultationSubject dept : depts) {
			tempList.add(dept.getSubjectId());
		}
		for (Iterator<LabConsultationSubject> iterator = depts.iterator(); iterator.hasNext();) {
			LabConsultationSubject dept = (LabConsultationSubject) iterator.next();
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(dept.getParentId())) {
				recursionFn(depts, dept);
				returnList.add(dept);
			}
		}
		if (returnList.isEmpty()) {
			returnList = depts;
		}
		return returnList;
	}

	/**
	 * 构建前端所需要下拉树结构
	 * 
	 * @param depts
	 *            部门列表
	 * @return 下拉树结构列表
	 */
	// @Override
	public List<TreeSelect> buildDeptTreeSelect(List<LabConsultationSubject> depts) {
		List<LabConsultationSubject> deptTrees = buildDeptTree(depts);
		return deptTrees.stream().map(TreeSelectSubject::new).collect(Collectors.toList());
	}

	/**
	 * 递归列表
	 */
	private void recursionFn(List<LabConsultationSubject> list, LabConsultationSubject t) {
		// 得到子节点列表
		List<LabConsultationSubject> childList = getChildList(list, t);
		t.setChildren(childList);
		for (LabConsultationSubject tChild : childList) {
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<LabConsultationSubject> getChildList(List<LabConsultationSubject> list, LabConsultationSubject t) {
		List<LabConsultationSubject> tlist = new ArrayList<LabConsultationSubject>();
		Iterator<LabConsultationSubject> it = list.iterator();
		while (it.hasNext()) {
			LabConsultationSubject n = (LabConsultationSubject) it.next();
			if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getSubjectId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<LabConsultationSubject> list, LabConsultationSubject t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

	/**
	 * 修改该部门的父级部门状态
	 * 
	 * @param dept
	 *            当前部门
	 */
	private void updateParentDeptStatus(LabConsultationSubject dept) {
		String updateBy = dept.getUpdateBy();
		dept = labConsultationSubjectMapper.selectLabConsultationSubjectById(dept.getSubjectId());
		dept.setUpdateBy(updateBy);
		labConsultationSubjectMapper.updateStatus(dept);
	}

	/**
	 * 修改子元素关系
	 * 
	 * @param deptId
	 *            被修改的部门ID
	 * @param newAncestors
	 *            新的父ID集合
	 * @param oldAncestors
	 *            旧的父ID集合
	 */
	public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
		List<LabConsultationSubject> children = labConsultationSubjectMapper.selectChildrenById(deptId);
		for (LabConsultationSubject child : children) {
			child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
		}
		if (children.size() > 0) {
			labConsultationSubjectMapper.updateChildren(children);
		}
	}

}
